package cn.lisens.demo.web;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.document.AbstractPdfView;
import org.springframework.web.servlet.View;

import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;

import cn.lisens.demo.views.PdfView;

@Controller
public class PDFController {
	
	
	@GetMapping("/export/pdf2")
    public ModelAndView exportPdf2(String userName,HttpServletResponse response){
		response.setHeader("Content-disposition", "attachment; FILENAME=someName2.pdf");
        ModelAndView mv = new ModelAndView(new PdfView());
        mv.addObject("wordList",Arrays.asList("张三","李四","office","Let's go"));
        return mv;
    }
	
	@GetMapping("/export/pdf")
    public ModelAndView exportPdf(String userName){
        View view= new AbstractPdfView() {
			@Override
			protected void buildPdfDocument(Map<String, Object> model, Document document, PdfWriter writer,
					HttpServletRequest request, HttpServletResponse response) throws Exception {
				BaseFont font = BaseFont.createFont("STSong-Light",  "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
				Font chinese = new Font(font,12);
				List<String> words = (List<String>) model.get("wordList");
				for (String word : words) {
					document.add(new Paragraph(word,chinese));
				}
			}
        };
        ModelAndView mv = new ModelAndView(view);
        mv.addObject("wordList",Arrays.asList("张三","李四","office","Let's go"));
        return mv;
    }
}
